require "common"
local nDltMapping ={{-1, -1, 8},{-1, -1, 8},{-1, 8, 7},{8, 7, 6},{6, 5, 4},{3, 2, 1}};
--判断字符串
function judge_str( num , max)
        local out = ""
        local st = {}
        if num == nil then return nil end
        local len = string.len( num )
        if len%2 ~= 0 then
                return nil
        end

        for i=1,len,2 do
                c = string.sub(num,i,i+1)
                if tonumber(c) > max or tonumber(c) <1 then return nil end
                if not in_table( c, st ) then
                        table.insert( st, c )
                else
                        return nil
                end
        end

        return true
end
--====================复式计算=============================================
function DltCompute(nTotalRed, nMaxBingoRed, nTotalBlue, nMaxBingoBule)
	local nBingo = {0,0,0,0,0,0,0,0}

	if nTotalRed < 5 or nTotalBlue < 2 or nTotalRed < nMaxBingoRed or nTotalBlue < nMaxBingoBule or nDltMapping[nMaxBingoRed+1][nMaxBingoBule+1] == -1 then
		return nBingo
	end

	for i=0,nMaxBingoRed do
		for j=0,nMaxBingoBule do
			if	nDltMapping[i+1][j+1] ~= -1  then
				if	j == 2 then
					nBingo[nDltMapping[i+1][j+1]] = nBingo[nDltMapping[i+1][j+1]] + combination(nTotalRed - nMaxBingoRed, 5 - i) * combination(nMaxBingoRed, i)
				elseif j == 1 then
					nBingo[nDltMapping[i+1][j+1]] = nBingo[nDltMapping[i+1][j+1]] + combination(nTotalRed - nMaxBingoRed, 5 - i) * combination(nMaxBingoRed, i) * combination(nTotalBlue - nMaxBingoBule, 1) * nMaxBingoBule
				else
					nBingo[nDltMapping[i+1][j+1]] = nBingo[nDltMapping[i+1][j+1]] + combination(nTotalRed - nMaxBingoRed, 5 - i) * combination(nMaxBingoRed, i) * combination(nTotalBlue - nMaxBingoBule, 2)
				end
			end
		end
	end
	return nBingo


end
--======================胆拖计算===============================================
function DltComputeDT(nTotalRedDT, nBingoRedDT, nTotalBlueDT, nBingoBuleDT,nTotalRed, nMaxBingoRed, nTotalBlue, nMaxBingoBule)
	local nBingo = {0,0,0,0,0,0,0,0}
	if nTotalRedDT + nTotalRed < 5 or nTotalBlueDT + nTotalBlue < 2 or nTotalRedDT > 4 or nTotalBlueDT > 1 or nTotalRed < nMaxBingoRed
	or nTotalBlue < nMaxBingoBule or nTotalRedDT < nBingoRedDT or nTotalBlueDT < nBingoBuleDT then
		return nBingo
	end
	for i=0,nMaxBingoRed do
		for j=0,nMaxBingoBule do
			if i + nTotalRedDT > 5 or j + nTotalBlueDT > 2 or nDltMapping[i + nBingoRedDT + 1][j + nBingoBuleDT + 1] == -1 then
				break
			else
				if j == 2 then
					nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i)
				elseif j == 1 then
					if nTotalBlueDT == 0 then
						nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i) * (nTotalBlue - nMaxBingoBule) * nMaxBingoBule
					else
						nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i) * nMaxBingoBule
					end
				else
					if nTotalBlueDT == 0 then
						nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i) * combination(nTotalBlue - nMaxBingoBule, 2)
					elseif nTotalBlueDT == 1 then
						nBingo[nDltMapping[i + nBingoRedDT + 1][j + nBingoBuleDT + 1]] = nBingo[nDltMapping[i + nBingoRedDT + 1][j + nBingoBuleDT + 1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i) * (nTotalBlue - nMaxBingoBule)
					else
						nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] = nBingo[nDltMapping[i + nBingoRedDT+1][j + nBingoBuleDT+1]] + combination(nTotalRed - nMaxBingoRed, 5 - nTotalRedDT - i) * combination(nMaxBingoRed, i)

					end
				end

			end
		end
	end

	return nBingo
end


--返回注数,倍投数 , 投注串, 指令串
function random_225( butType, inStr )
	local butNum = 1
	local butPower = 1
	local zlStr = ""
	local butStr = ""
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )
	qjStr = string.gsub( inStr, "#B", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qjStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end
	--单式机选  22#注数#B倍数
	if butType == '1' then
		butNum = tonumber(itemList[1])
		for i=1,butNum do
			local nums = sel_dist_num(5,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)
			zlStr = zlStr..format_num(nums[1])..format_num(nums[2])..format_num(nums[3])..format_num(nums[4])..format_num(nums[5])

			if i<butNum then zlStr = zlStr.."#" end
		end
		zlStr = zlStr.."#B"..butPower
		butNum,butPower,butStr = check_225('1', zlStr)
	end

	return butNum, butPower, butStr, zlStr


end

--返回注数,倍投数,投注串
function check_225( butType, inStr)
	local qxStr;
	local qjStr;
	local butNum = 0;
	local butPower = 1;
	local butStr = "";
	local itemList = {}
	local itemLen = 0;
	itemList = Split( inStr, "#B" )
	itemLen = table.getn( itemList )

	qjStr = string.gsub( inStr, "#B", "0")
	qxStr = string.gsub( qjStr, "#", "0")
	if itemLen > 2 or itemLen < 1 or string.find(qxStr,"%D") then return 0 end

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower >99 or butPower < 1 then return 0 end

	--单式自选 221#注码#B倍数  221#0102030405#0405060708
	if  butType == '1' then
		xItem = Split(itemList[1], "#")
		butNum = #xItem
		if butNum > 5 or butNum < 1  then
			return 0
		end
		for i=1,#xItem do
			if string.len(xItem[i]) ~= 10 then return 0 end
			if not judge_str(xItem[i],22) then return 0 end
		end
	--复式自选 223#前区#后区  223#0203040506070809
	elseif butType == '2' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 1 or string.len(xItem[1])/2 < 6 or string.len(xItem[1])/2 > 22  then return 0 end
		butNum = combination(string.len(xItem[1])/2,5)
		if not judge_str(xItem[1],22)  then return 0 end
	--胆拖 	224#胆码#拖码
	elseif butType == '3' then
		xItem = Split(itemList[1], "#")
		if #xItem ~= 2 then return 0 end
		if string.len(xItem[1])/2 > 4 or (string.len(xItem[2])/2 + string.len(xItem[1])/2) < 6  then return 0 end
		if not judge_str(xItem[1],22) or not judge_str(xItem[2],22)   then return 0 end
		if  is_str_mix(xItem[1],xItem[2])  then return 0 end
		butNum = combination(string.len(xItem[2])/2,(5-string.len(xItem[1])/2))

	else
		return 0
	end;
	return butNum,butPower,inStr,inStr
end
--=====================================中奖查询==================================================
function query_225(butType,queryStr,djStr)
	local nBingo = {0,0,0,0,0,0,0,0}
	local temp = {0,0,0,0,0,0,0,0}
	local butPower = 1
	local itemList = {}
	local bullStr = {}
	local bingoNum = {}
	local qhStr = {}
	local nTotalRed = 0
	local nTotalBlue = 0
	local nTotalRedDT = 0
	local nTotalBlueDT = 0
	local nBingoRedDT = 0
	local nBingoBuleDT = 0
	itemList = Split( queryStr, "#B" )

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butType == '1' then
		jItem = Split(itemList[1], "#")
		nTotalRed = 5
		nTotalBlue = 2
		for i = 1,table.getn(jItem) do
			bingoNum[1] = ConputeBingoNum(jItem[i],djStr,2)
			bingoNum[2] = 2
			temp = nBingo
			nBingo = DltCompute(nTotalRed, bingoNum[1], nTotalBlue, bingoNum[2])
			for j = 1, 8 do
				nBingo[j] = nBingo[j] + temp[j]
			end
		end

	elseif butType == '2' then
		nTotalRed = string.len(itemList[1])/2
		nTotalBlue = 2
		bingoNum[1] = ConputeBingoNum(itemList[1],djStr,2)
		bingoNum[2] = 2
		nBingo = DltCompute(nTotalRed, bingoNum[1], nTotalBlue, bingoNum[2])

	elseif butType == '3' then
		jItem = Split(itemList[1], "#")
		--胆拖
		nTotalRedDT = string.len(jItem[1]) / 2
		nTotalRed = string.len(jItem[2]) / 2
		nTotalBlue = 2
		bingoNum[1] = ConputeBingoNum(jItem[1],djStr,2)
		bingoNum[2] = ConputeBingoNum(jItem[2],djStr,2)
		bingoNum[3] = 0
		bingoNum[4] = 2
		nBingo = DltComputeDT(nTotalRedDT,bingoNum[1],nTotalBlueDT,bingoNum[3],nTotalRed,bingoNum[2],nTotalBlue,bingoNum[4])
	end
	for i=1 ,8 do
		nBingo[i] = nBingo[i] * butPower
	end
	return nBingo[1]..","..nBingo[4]..","..nBingo[6]
end

--print(random_225('1',"5#B11"))
--print(check_225('1',"1008131401#0608131409#B10"))
--print(check_225('2',"01020304050607"))
--print(check_225('3',"100813#060912#B10"))
--print(query_225('1',"0102030405#0102030405#B10","0102030405"))
--print(query_225('3',"01020304#0506#B10","0102030405"))
--print(query_225('2',"0102030405060708#B10","0102030405"))
